package cn.buttheway.moodmusicdiary.utils;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.buttheway.moodmusicdiary.models.Song;

public class MusicApiHelper {

    private List<Song> positiveSongs;
    private List<Song> neutralSongs;
    private List<Song> negativeSongs;
    private Random random;

    public MusicApiHelper() {
        positiveSongs = new ArrayList<>();
        neutralSongs = new ArrayList<>();
        negativeSongs = new ArrayList<>();
        random = new Random();

        // 初始化一些示例歌曲
        positiveSongs.add(new Song("Happy", "Pharrell Williams"));
        positiveSongs.add(new Song("Don't Stop Me Now", "Queen"));
        positiveSongs.add(new Song("Walking on Sunshine", "Katrina and The Waves"));

        neutralSongs.add(new Song("Imagine", "John Lennon"));
        neutralSongs.add(new Song("The Sound of Silence", "Simon & Garfunkel"));
        neutralSongs.add(new Song("Wonderwall", "Oasis"));

        negativeSongs.add(new Song("Everybody Hurts", "R.E.M."));
        negativeSongs.add(new Song("Hurt", "Johnny Cash"));
        negativeSongs.add(new Song("The Sound of Silence", "Disturbed"));
    }

    public void getRecommendation(String sentiment, OnSuccessListener<Song> successListener, OnFailureListener failureListener) {
        List<Song> selectedList;
        switch (sentiment) {
            case "积极":
                selectedList = positiveSongs;
                break;
            case "消极":
                selectedList = negativeSongs;
                break;
            default:
                selectedList = neutralSongs;
        }

        if (!selectedList.isEmpty()) {
            Song recommendation = selectedList.get(random.nextInt(selectedList.size()));
            successListener.onSuccess(recommendation);
        } else {
            failureListener.onFailure(new Exception("No songs available for the given sentiment"));
        }
    }
}